package com.qingneng.budget.web;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.Action;
import com.qingneng.budget.service.sdk.IWorkBudgetService;
import com.qingneng.budget.util.BaseAction;
import com.qingneng.budget.util.UtilTool;
import com.qingneng.budget.vo.BudgetWorkDoInfo;

public class SaveBudgetDoInfoAction extends BaseAction {
	
	private static final Logger logger  = Logger.getLogger(SaveBudgetDoInfoAction.class);
	
	public IWorkBudgetService workBudgetService;
	
	public String rowlist  =  null;
	
	public int project_workinfo_id;
	
	
	public int getProject_workinfo_id() {
		return project_workinfo_id;
	}
	public void setProject_workinfo_id(int project_workinfo_id) {
		this.project_workinfo_id = project_workinfo_id;
	}
	public String getRowlist() {
		return rowlist;
	}
	public void setRowlist(String rowlist) {
		this.rowlist = rowlist;
	}
	public SaveBudgetDoInfoAction(){
		
	}
	public SaveBudgetDoInfoAction(IWorkBudgetService workBudgetService){
		this.workBudgetService = workBudgetService;
	}
	
	private String message;
	
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public String execute(){
		if(logger.isDebugEnabled()){
			logger.debug("+++begin to execute SaveBudgetDoInfoAction +++ ");
		}
		logger.info("rowlist:"+rowlist);
		logger.info("project_workinfo_id:"+project_workinfo_id);
		if(rowlist!=null && !"".equals(rowlist)){
			//先删除 当前工程下的预算结果
			BudgetWorkDoInfo temp_bean = new BudgetWorkDoInfo();
			temp_bean.setProject_workinfo_id(project_workinfo_id);
			temp_bean.setParent_item_id(0);
			try{
				workBudgetService.destroySubBudgetWorkDoInfo(temp_bean);
				//进行新增
				String[] rows = rowlist.split("\\|");
				if(rows!=null && rows.length>0){
					for(int i=0;i<rows.length;i++){
						String budgetInfo_jsonString = rows[i];
						logger.info("budgetInfo_jsonString:"+budgetInfo_jsonString);
						BudgetWorkDoInfo  bean =  (BudgetWorkDoInfo)UtilTool.parseJsonStr2Obj(budgetInfo_jsonString,BudgetWorkDoInfo.class);
						//JSONObject jo = JSONObject.fromObject(budgetInfo_jsonString);// 格式化成json对象
						bean.setParent_item_id(0);
						bean.setProject_workinfo_id(project_workinfo_id);
						workBudgetService.addBudgetWorkDoInfo(bean);
					}
				}
			message = "true";
			}catch (Exception e) {
				e.printStackTrace();
				message = "false";
			}
			
		}
		
		
		if(logger.isDebugEnabled()){
			logger.debug("+++end to execute SaveBudgetDoInfoAction +++ ");
		}
		return Action.SUCCESS;
	}

}
